#include "client.h"
int main(int argc, char *argv[]){   

    ARGS_CHECK(argc,3);
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    addr.sin_addr.s_addr = inet_addr(argv[1]);
    int ret = connect(sockfd,(struct sockaddr*)&addr,sizeof(addr));
    ERROR_CHECK(ret,-1,"connect");

    int epfd = epoll_create(1);

    //监听键盘输入
    struct epoll_event event;
    event.events = EPOLLIN;
    event.data.fd = STDIN_FILENO;
    epoll_ctl(epfd,EPOLL_CTL_ADD,STDIN_FILENO,&event);

    char buf[1024];
    struct epoll_event readyset[1024];

    printf("sockfd connect\n");
    while(1){
        int readynum = epoll_wait(epfd,readyset,1024,-1);
        for(int i=0;i<readynum;++i){
           // printf("stdin success\n");
            if(readyset[i].data.fd==STDIN_FILENO){
                bzero(buf,sizeof(buf));
                read(STDIN_FILENO,buf,sizeof(buf));
               // printf("rer_read = %d\n",ret_read);
                char lineValue[1024];
                bzero(lineValue,sizeof(lineValue));
                //短命令的传输
                //返回值type是命令类型，把两个长命令单独拿出来
                int type = commandManage(buf,sockfd,lineValue);
                //上传
                if(type == 21){
                      printf("puts\n");
                      printf("%s\n",lineValue);
                      int ret_puts = up_file(sockfd,lineValue);
                      if(ret_puts == -1){
                         printf("puts failed\n");
                      }
                }
                //下载
                if(type == 25){
                    int ret_gets = down_file(sockfd,lineValue);
                    if(ret_gets == -1){
                        printf("gets failed\n");
                    }
                }

            }
        }
    }
    return 0;
}
